{% extends 'base.html' %}

{% block title %}备件库存管理 - 维修管理系统{% endblock %}

{% block content %}
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h3 class="card-title mb-0">备件库存管理</h3>
                    <div>
                        <a href="{% url 'spare_parts:inventory_add' %}" class="btn btn-primary btn-sm">
                            📦 入库操作
                        </a>
                        <a href="{% url 'spare_parts:usage_add' %}" class="btn btn-warning btn-sm ms-2">
                            🛠️ 使用记录
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <!-- 搜索和过滤表单 -->
                    <form method="get" class="row g-3 mb-4">
                        <div class="col-md-4">
                            <input type="text" name="q" class="form-control" placeholder="搜索备件名称、编号或型号..." 
                                   value="{{ search_query }}">
                        </div>
                        <div class="col-md-3">
                            <select name="category" class="form-select">
                                <option value="">所有分类</option>
                                {% for category in categories %}
                                <option value="{{ category.id }}" {% if selected_category == category.id|stringformat:"s" %}selected{% endif %}>
                                    {{ category.name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-3">
                            <select name="status" class="form-select">
                                <option value="">所有状态</option>
                                <option value="low" {% if selected_status == 'low' %}selected{% endif %}>库存不足</option>
                                <option value="high" {% if selected_status == 'high' %}selected{% endif %}>库存过高</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-primary w-100">搜索</button>
                        </div>
                    </form>

                    <!-- 库存列表 -->
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>备件编号</th>
                                    <th>名称</th>
                                    <th>型号</th>
                                    <th>分类</th>
                                    <th>当前库存</th>
                                    <th>库存状态</th>
                                    <th>最低库存</th>
                                    <th>最高库存</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for part in spare_parts %}
                                <tr>
                                    <td><strong>{{ part.part_number }}</strong></td>
                                    <td>{{ part.name }}</td>
                                    <td>{{ part.model }}</td>
                                    <td>
                                        {% if part.category %}
                                        <span class="badge bg-info">{{ part.category.name }}</span>
                                        {% else %}
                                        <span class="text-muted">未分类</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <strong class="{% if part.is_low_stock %}text-danger{% elif part.is_over_stock %}text-warning{% else %}text-success{% endif %}">
                                            {{ part.current_stock }} {{ part.unit }}
                                        </strong>
                                    </td>
                                    <td>
                                        {% if part.is_low_stock %}
                                        <span class="badge bg-danger">❌ 库存不足</span>
                                        {% elif part.is_over_stock %}
                                        <span class="badge bg-warning">⚠️ 库存过高</span>
                                        {% else %}
                                        <span class="badge bg-success">✅ 正常</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ part.min_stock }} {{ part.unit }}</td>
                                    <td>
                                        {% if part.max_stock > 0 %}
                                        {{ part.max_stock }} {{ part.unit }}
                                        {% else %}
                                        <span class="text-muted">无限制</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <span class="badge 
                                            {% if part.status == 'active' %}bg-success
                                            {% elif part.status == 'discontinued' %}bg-warning
                                            {% else %}bg-secondary{% endif %}">
                                            {{ part.get_status_display }}
                                        </span>
                                    </td>
                                    <td>
                                        <a href="{% url 'spare_parts:inventory_detail' part.pk %}" class="btn btn-info btn-sm">
                                            详情
                                        </a>
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="10" class="text-center text-muted py-4">
                                        {% if search_query or selected_category or selected_status %}
                                        没有找到匹配的备件
                                        {% else %}
                                        暂无备件数据
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>

                    <!-- 库存统计 -->
                    {% if spare_parts %}
                    <div class="row mt-4">
                        <div class="col-md-4">
                            <div class="card bg-light">
                                <div class="card-body text-center">
                                    <h5 class="card-title">总备件数</h5>
                                    <h3 class="text-primary">{{ spare_parts|length }}</h3>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card bg-light">
                                <div class="card-body text-center">
                                    <h5 class="card-title">库存不足</h5>
                                    <h3 class="text-danger">
                                        {{ spare_parts|length|add:"0" }}
                                    </h3>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card bg-light">
                                <div class="card-body text-center">
                                    <h5 class="card-title">库存过高</h5>
                                    <h3 class="text-warning">
                                        {{ spare_parts|length|add:"0" }}
                                    </h3>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.table th {
    background-color: #2c3e50;
    color: white;
}
.card-header {
    background-color: #2c3e50;
    color: white;
}
.badge {
    font-size: 0.85em;
}
</style>
{% endblock %}
